package com.liang.controller;

import com.liang.rlock.AquiredLockWorker;
import com.liang.rlock.DistributedLocker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Created with Intellij IDEA
 *
 * @Auther: liangjy
 * @Date: 2022/01/06/17:31
 * @Description:
 */
@RestController
public class RedisController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DistributedLocker distributedLocker;
    @RequestMapping(value = "index")
    public String index() throws Exception {
        distributedLocker.lock("test", new AquiredLockWorker<Object>(){
            @Override
            public Object invokeAfterLockAquire() throws Exception {
                redisTemplate.opsForValue().set("key","test");
                System.out.println("这里直接进行逻辑处理");
                Thread.sleep(10000);
                return null;
            }
        });
        return "hello redis";
    }
}
